package edu.maping.easy.model;

import java.util.ArrayList;

import edu.maping.easy.painter.PainterDelegateContainer;
import edu.maping.easy.painter.ThingPainterDelegate;
import edu.maping.easy.painter.plugins.ThingPainterContournTransform;
import edu.maping.easy.painter.plugins.ThingPainterRectangleTransform;
import processing.core.PApplet;
import processing.xml.XMLElement;

public abstract class Thing extends Point {

	private boolean event = false;
	public boolean active = true;

	private long id = -1;

	private static Long count = null;

	public static String XML_NAME = "Thing";

	public Thing() {
		super(0, 0, false);
		// TODO Auto-generated constructor stub
		id = nextValue();
	}

	public Thing(int x, int y) {
		super(x, y);
		id = nextValue();
	}

	private static synchronized long nextValue() {
		if (count == null) {
			count = 0L;
		} else {
			count++;
		}
		long next = count.longValue();
		return next;
	}

	public void draw(PApplet pApplet) {
		// for (PainterDelegateContainer container : painterDelegate) {
		// if (container.active)
		// container.painterDelegate.draw(this, pApplet);
		// }
	}

	public void moveXY(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public boolean isEvent() {
		return event;
	}

	public void setEvent(boolean event) {
		this.event = event;
	}

	// public ArrayList<PainterDelegateContainer> painterDelegate = new
	// ArrayList<PainterDelegateContainer>();

	public abstract void editDraw(PApplet applet);

	public XMLElement startXmlization() {
		XMLElement xmlElement = new XMLElement();
		xmlElement.setAttribute("id", "" + id);
		xmlElement.setAttribute("x", "" + x);
		xmlElement.setAttribute("y", "" + y);
		xmlElement.setName(XML_NAME);
		return xmlElement;
	}

	protected abstract Thing newElement();

	public abstract Thing copy();
	
	public abstract void refresh();
	
	public Thing startDesXmlization(XMLElement father) {

		Thing thing = newElement();
		String stringX = father.getAttribute("x");
		if (stringX != null) {
			x = Integer.parseInt(stringX);
			thing.x = x;
		}

		String stringY = father.getAttribute("y");
		if (stringY != null) {
			y = Integer.parseInt(stringY);
			thing.y = y;
		}

		String stringId = father.getAttribute("id");
		if (stringId != null) {
			id = Long.parseLong(stringId);
			thing.id = id;
		}

		return thing;

	}

	public XMLElement endXmlization(XMLElement xmlElement) {

		return xmlElement;
	}

	/**
	 * @return the id
	 */
	public long getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}

}
